--[[
--
--	Gaius iChat -- Macro enhancer to communication Messages
--
--]]

local unit="target";
local result;
local macroModifier = {
 ["target=(%w*)."]			= function(UnitId) -- Use targeting for thIs Macro scope
									if UnitId ~= nil and UnitId ~= "" then 
										unit = string.lower(UnitId) 
										return ""
									else
										unit = "target"
										return ""
									end
								end,
 ["(%w*)exists"]			= function(negator)
								if UnitExists(unit) == 1 then
									if negator ~= nil or negator ~= "" then
										return "false"
									end
									return "true"
								else
									if negator ~= nil or negator ~= "" then
										return "true"
									end
									return "false"
								end
							end,
 ["(%w*)dead"]				= function(negator)
								if UnitIsDead(unit) then
									if negator ~= nil or negator ~= "" then
										return "false"
									else
										return "true"
									end
								else
									if negator ~= nil or negator ~= "" then
										return "true"
									else
										return "false"
									end
								end
							end,
 ["(%w*)stance:(%s*)"]		= function(negator, StanceId)
								StanceId = string.gsub(StanceId,"/"," or ")
								if GetShapeshiftForm(loadstring(StanceId)) then
									if negator ~= nil or negator ~= "" then
										return "false"
									end
									return "true"
								else
									if negator ~= nil or negator ~= "" then
										return "true"
									end
									return "false"
								end
							end,
 ["(%w*)stealth"]			= function(negator)
								if IsStealthed() then
									if negator ~= nil or negator ~= "" then
										return "false"
									else
										return "true"
									end
								else
									if negator ~= nil or negator ~= "" then
										return "true"
									else
										return "false"
									end
								end

							end,
 ["(%w*)help"]				= function(negator)
								if UnitIsFriend("player",unit) == 1 then
									if negator ~= nil or negator ~= "" then
										return "false"
									else
										return "true"
									end
								else
									if negator ~= nil or negator ~= "" then
										return "true"
									else
										return "false"
									end
								end

							end,
 ["(%w*)harm"]				= function(negator)
								if UnitIsFriend("player",unit) == nil then
									if negator ~= nil or negator ~= "" then
										return "false"
									else
										return "true"
									end
								else
									if negator ~= nil or negator ~= "" then
										return "true"
									else
										return "false"
									end
								end
							end,
 ["(%w*)modifier(%w*)"] 	= function(negator, modifier)
								if modifier == nil or modifier == "" then
									if IsModifierKeyDown() then 
										if negator ~= nil or negator ~= "" then
											return "false"
										else
											return "true"
										end
									end
								end
								if IsModifierKeyDown() then
									ChatFrame1:AddMessage("Checking Modifier Key")
									if IsShiftKeyDown() and string.find(string.lower(modifier),"shift") ~= nil then
										if negator ~= nil or negator ~= "" then
											return "false"
										else
											return "true"
										end
								elseif IsControlKeyDown() and string.find(string.lower(modifier),"ctrl") ~= nil then
										if negator ~= nil or negator ~= "" then
											return "false"
										else
											return "true"
										end
								elseif IsAltKeyDown() and string.find(string.lower(modifier),"alt") ~= nil then
										if negator ~= nil or negator ~= "" then
											return "false"
										else
											return "true"
										end
					
								elseif IsModifiedClick(string.upper(modifier)) ~= nil then
										if negator ~= nil or negator ~= "" then
											return "false"
										else
											return "true"
										end
									end
								end
								return "false"
							end,	
 ["(%w*)button:(%w*)"]  	= function(negator,mouseButton) 
								mouseButton = string.gsub(mouseButton,"/"," or ")
								
								if IsMouseButtonDown(loadstring(mouseButton)) then 
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							end,
 ["(%w*)equipped:(%w*)"] 	= function(negator, inventorySlot)
								if GetInventorySlotInfo(string.upper(inventorySlot).."SLOT") then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)channeling:(%w*)"] 	= function(negator, SpellName)
								if select(1, UnitChannelInfo(unit)) == SpellName then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)actionbar:(%w*)"]	= function(negator, ActionbarId)
								ActionbarId = string.gsub(ActionbarId,"/"," or ")
								if GetActionBarPage() == loadstring(ActionbarId) then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)pet:(%w*)"]			= function(negator, pet)
								if UnitCreatureType(pet) then
									result =  true
								elseif UnitName("pet") then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)combat"] 			= function(negator)
								if UnitAffectingCombat(unit) then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							 end,
 ["(%w*)mounted"] 			= function(negator)
								if IsMounted() then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)swimming"] 			= function(negator)
 								if IsSwimming() then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)flying"] 			= function(negator)
 								if IsFlying() then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)flyable"] 			= function(negator)
								SetMapToCurrentZone()
 								if GetCurrentMapContinent() > 2 then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)indoors"] 			= function(negator)
 								if IsIndoors() then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)outdoors"] 			= function(negator)
 								if IsOutdoors() then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)party"] 			= function(negator)
 								if UnitInParty(unit) then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)raid"] 				= function(negator)
 								if UnitInRaid(unit) then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
 ["(%w*)group(%w*)"] 		= function(negator, GroupType)
 								if string.lower(GroupType) == "party" and UnitInParty("player") then
									result =  true
								else
									result =  false
								end
 								if string.lower(GroupType) == "raid" and UnitInRaid("player") then
									result =  true
								else
									result =  false
								end
								if negator ~= nil or negator ~= "" and result == true then
									return "false"
								end
								if negator ~= nil or negator ~= "" and result == false then
									return "true"
								end
								if result == true then
									return "true"
								else
									return "false"
								end
							  end,
};

--
-- iChat will replace the game intern SendChatMessage and will add the ability to use macro-based modifier in chat Messages
--

local SavedSendChatMessage = SendChatMessage

function iChat_SendChatMessage(msg, system, language, channel)
-- exampel macro:
--
-- /say [mounted, flyable, noflying] Why i am not flying ? ;[ ] I have nothing to say, really !
--

	local globalScope;

	local macroScope = select(3,string.find(msg, "(%b[])"))
	if string.find(msg,"%b[]") then
	
	local macroScopeE = string.gsub(macroScope,"%[","%%[")
	local macroScopeE = string.gsub(macroScopeE,"%]","%%]")
	
	msg = string.gsub(msg, macroScopeE, "")
			
	for conditional, func in pairs(macroModifier) do
		macroScope = string.gsub(macroScope, conditional, func)
		
	end 
			
	macroScope = string.gsub(macroScope, ",", "and ")
	macroScope = string.gsub(macroScope, "/", " or ")
	macroScope = string.sub(macroScope, 2, -2)

	unit = "target"
	ChatFrame1:AddMessage(macroScope.." @"..unit)
	if assert(loadstring("if "..macroScope.." == true then return 1 end"))() then
		globalScope = true
	else
		globalScope = false
	end
			
	if globalScope == true then
		
		
		SavedSendChatMessage(msg, system, language, channel)
	end
	else
		SavedSendChatMessage(msg, system, language, channel)
	end
end

SendChatMessage = iChat_SendChatMessage
